﻿using System;
using System.Linq;
using System.Text;
using ProtoBuf;

namespace LingDong.IndexEntities.Document
{
    [ProtoContract]
    [Serializable]
    public class ParsedDocumentEntity : DocumentEnity
    {
        #region Constructor

        private ParsedDocumentEntity()
        {
            this.SentenceList = new string[0];
        }

        public ParsedDocumentEntity(int docID, string url, string title, string content)
            : base(docID, url)
        {
            this.Rank = 0.0;
            this.Title = title;
            this.SentenceList = content.Split(ConfigProvider.SentenceSplitter.Get(), 
                                              StringSplitOptions.RemoveEmptyEntries).
                                        Select(sen => sen.Trim()).ToArray();
        }

        #endregion
        

        #region ToString
        
        public override string ToString()
        {
            StringBuilder sentence = new StringBuilder();
            foreach (string sen in SentenceList)
            {
                sentence.AppendLine(sen);
            }
            return String.Format("DocID: {0}, URL: {1}, Title: {2}, Rank: {3}, SentenceList: \n{4}",
                DocID, URL, Title, Rank, sentence.ToString());
        }

        #endregion

        
        #region Feilds
        
        /// <summary>
        /// Document Title
        /// </summary>
        [ProtoMember(1)]
        public string Title { get; set; }

        /// <summary>
        /// Document rank, [0, 1]
        /// It depends on two pars: doc quality, link analysis
        /// The value is set in subindex merger
        /// </summary>
        [ProtoMember(2)]
        public double Rank { get; set; }

        /// <summary>
        /// Sentences of the document
        /// </summary>
        [ProtoMember(3)]
        public string[] SentenceList { get; set; }

        #endregion
    }
}
